import C from "../Configs/Config";
import ClickItem from "./ClickItem";
import SpriteItem from "./SpriteItem";
import IAsset from "../Interfaces/IAsset";
import EventDefines from "../Constants/EventDefines";

/**
 * 编辑内容view
 * created by Five on 2018-11-5 18:35:34
 */
const { ccclass } = cc._decorator;

@ccclass
export default class ItemView extends cc.Component {

    public Scroll: cc.ScrollView;
    public Content: cc.Node;

    start() {
        this.Scroll = this.node.getComponent(cc.ScrollView);
        this.Content = this.Scroll.content;

        this.init();
        this.node.on(cc.Node.EventType.TOUCH_START, (e:cc.Event.EventTouch) => {
            e.stopPropagation();
            this.node.dispatchEvent(new cc.Event(EventDefines.ENTITY_CLEAR, true));
            return true;
        }, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, (e:cc.Event.EventTouch) => {
            e.stopPropagation();
        }, this);
        this.node.on(cc.Node.EventType.TOUCH_END, (e:cc.Event.EventTouch) => {
            e.stopPropagation();
        }, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, (e:cc.Event.EventTouch) => {
            e.stopPropagation();
        }, this);
    }

    private init() {
        C().MapAsset.Assets.forEach(asset => {
            this.loadEntity(asset);
        })
        C().Monster.Assets.forEach(asset => {
            this.loadEntity(asset);
        })
    }

    private async loadEntity(asset: IAsset) {
        let node = new cc.Node();
        node.addComponent(SpriteItem).SetSpriteName(asset.asset);
        node.addComponent(ClickItem).SetData(asset);
        node.scale = 0.5;
        this.Content.addChild(node);
    }
}
